#ifndef __TRAIN_TICKET_H__
#define __TRAIN_TICKET_H__

#include "list.h"
/*函数返回值*/
#define FALSE                   (0)
#define TRUE                    (1)

enum {
    TICKET_PARAM_NULL = 1,
    TICKET_OPEN_FILE_FAIL,
    TICKET_WRITE_FILE_FAIL,
    TRAIN_ENCIPHER_FAIL,
    TRAIN_DECIPHER_FAIL,
};

/**
 * 车票信息处理
 */
#define TICKET_ID_LENGTH        (6)
#define CITY_LENGTH             (10)
#define TIME_LENGTH             (6)
#define TOTAL_TICKET_NUM        (50)

#define TRAIN_TICKET_FILE   "./lib/train_ticket.txt"
#define USER_INFO_FILE      "./lib/user_info.txt"
#define LIB_DIR             "./lib"

struct train_ticket {
    char id[TICKET_ID_LENGTH];
    char start_city[CITY_LENGTH];
    char end_city[CITY_LENGTH];
    char departure_time[TIME_LENGTH];
    char arrival_time[TIME_LENGTH];
    char ticket_price[4];
    char spare_ticket_num[4];
    char total_ticket_num[4];
    struct list_head list;

};

/**
 * 用户信息处理
 */
#define USER_NAME_LENGTH        (11)
#define USER_ACCOUNT_LENGTH     (11)
#define PHONE_NUN_LENGTH        (12)
#define HOME_ADDRESS_LENGTH     (11)
#define OWN_TICKET_MAX          (5)
#define TICKET_ID_LENGTH        (6)

struct booked_ticket_id {
    char id[TICKET_ID_LENGTH];
    struct list_head list;
};

struct user_info {
    char age[4];
    char sex[2];
    char name[USER_NAME_LENGTH];
    char account[USER_ACCOUNT_LENGTH];
    char phone_num[PHONE_NUN_LENGTH];
    char home_address[HOME_ADDRESS_LENGTH];
    char own_ticket[OWN_TICKET_MAX][TICKET_ID_LENGTH];
    char is_have_info;
    unsigned int own_ticket_count;
};


extern int train_ticket_init(struct list_head *p_ticket_head, 
                             struct list_head *p_booked_ticket_id_head);

/**
 * 车票信息处理
 */
extern int add_train_ticket(struct list_head *p_ticket_head, struct train_ticket train_ticket_data);
extern int delete_train_ticket(struct list_head *p_ticket_head);
extern struct train_ticket *search_train_ticket(struct list_head *p_ticket_head);
extern int display_train_ticket(struct list_head *p_ticket_head, int page, int num);
/**
 * 用户信息处理
 */
extern int load_user_info(char *account, 
                          struct user_info *p_user_info, 
                          struct list_head *p_booked_ticket_id_head);
extern int logout_user_info(struct user_info *p_user_info, 
                            struct list_head *p_booked_ticket_id_head);
extern int add_user_info(struct user_info *p_user_info, char *name, char *account);
extern int change_user_info(struct user_info *p_user_info,
                            struct list_head *p_booked_ticket_id_head);
extern int display_user_ticket_id(struct list_head *p_booked_ticket_id_head);
extern int display_user_ticket(struct list_head *p_booked_ticket_id_head, 
                               struct list_head *p_ticket_head);
/**
 * 购票退票处理
 */
extern int book_train_ticket(struct user_info *p_user_info, 
                             struct list_head *p_booked_ticket_id_head, 
                             struct list_head *p_ticket_head, 
                             char *id);
extern int return_train_ticket(struct user_info *p_user_info, 
                               struct list_head *p_booked_ticket_id_head, 
                               struct list_head *p_ticket_head, 
                               char *id);

extern int train_ticket_destroy(struct list_head *p_ticket_head, 
                                struct user_info *p_user_info, 
                                struct list_head *p_booked_ticket_id_head);

#endif